package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPicker;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;
import com.sendpo.zjx.sendpo.views.MainScrollview;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/15.
 */

public class YinPinzheUserActivity extends AppCompatActivity {
    public Integer jobId = 0;
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.editname_iv)
    ImageView editnameIv;
    @BindView(R.id.editname_tv)
    TextView editnameTv;
    @BindView(R.id.editname_bt)
    RelativeLayout editnameBt;
    @BindView(R.id.sex_iv)
    ImageView sexIv;
    @BindView(R.id.sex_tv)
    TextView sexTv;
    @BindView(R.id.sex_edit)
    EditText sexEdit;
    @BindView(R.id.sex_bt)
    RelativeLayout sexBt;
    @BindView(R.id.editphone_iv)
    ImageView editphoneIv;
    @BindView(R.id.editphone_tv)
    TextView editphoneTv;
    @BindView(R.id.editphone_bt)
    RelativeLayout editphoneBt;
    @BindView(R.id.editemail_iv)
    ImageView editemailIv;
    @BindView(R.id.editemail_tv)
    TextView editemailTv;
    @BindView(R.id.editemail_bt)
    RelativeLayout editemailBt;
    @BindView(R.id.city_iv)
    ImageView cityIv;
    @BindView(R.id.city_tv)
    TextView cityTv;
    @BindView(R.id.city_content)
    TextView cityContent;
    @BindView(R.id.city_bt)
    RelativeLayout cityBt;
    @BindView(R.id.xueli_iv)
    ImageView xueliIv;
    @BindView(R.id.xueli_tv)
    TextView xueliTv;
    @BindView(R.id.jobeducation_edit)
    EditText jobeducationEdit;
    @BindView(R.id.xueli_bt)
    RelativeLayout xueliBt;
    @BindView(R.id.exprience_iv)
    ImageView exprienceIv;
    @BindView(R.id.exprience_tv)
    TextView exprienceTv;
    @BindView(R.id.jobname_experience)
    EditText jobnameExperience;
    @BindView(R.id.exprience_bt)
    RelativeLayout exprienceBt;
    @BindView(R.id.works_iv)
    ImageView worksIv;
    @BindView(R.id.works_tv)
    TextView worksTv;
    @BindView(R.id.works_content)
    TextView worksContent;
    @BindView(R.id.works_bt)
    RelativeLayout worksBt;
    @BindView(R.id.works_content_edit)
    EditText worksContentEdit;
    @BindView(R.id.maincrollview)
    MainScrollview maincrollview;
    @BindView(R.id.realname_edit)
    EditText realnameEdit;
    @BindView(R.id.phone_edit)
    EditText phoneEdit;
    @BindView(R.id.email_edit)
    EditText emailEdit;
    private CityPicker cityPicker;
    private String toprovince = "";
    private String tocity = "";
    private String toarea = "";
    private Dialog loadingdialog=null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yinpin_userinfo);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        jobId = getIntent().getIntExtra("jobId", 0);
        cityPicker = new CityPicker.Builder(this)
                .textSize(20)
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#E6212A")
                .titleTextColor("#FFFFFF")
                .confirTextColor("#FFFFFF")
                .cancelTextColor("#FFFFFF")
                .province("广东省")
                .city("深圳市")
                .district("福田区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... contents) {
                cityContent.setText(contents[0] + contents[1] + contents[2]);
                toprovince = contents[0];
                tocity = contents[1];
                toarea = contents[2];
            }

            @Override
            public void onCancel() {

            }
        });
        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.setCanceledOnTouchOutside(false);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @OnClick(value = {R.id.backbt, R.id.city_bt, R.id.submit_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.city_bt:
                cityPicker.show();
                break;
            case R.id.submit_bt:
                if (TextUtils.isEmpty(realnameEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(sexEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写性别男或女", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(sexEdit.getText().equals("男")||sexEdit.getText().equals("女")){
                    Toast.makeText(getApplicationContext(), "请填写正确的性别，男或女", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(phoneEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写联系方式", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(emailEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写邮箱", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(cityContent.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "选择城市不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(jobeducationEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写学历", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(jobnameExperience.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写工作经验", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(worksContentEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "请填写工作经历", Toast.LENGTH_SHORT).show();
                    return;
                }


                List<User> userList= Xutils.queryAll(getApplication(),User.class);
                  if(userList==null){
                  Tools.redirectActivity(this,LoginActivity.class);
                      return;
                  }
                save(userList.get(0).getUserId()+"",
                        jobId+"",
                        realnameEdit.getText().toString().trim(),
                        sexEdit.getText().toString().trim(),
                        phoneEdit.getText().toString().trim(),
                        emailEdit.getText().toString().trim(),
                        toprovince,
                        tocity,
                        toarea,
                        jobeducationEdit.getText().toString().trim(),
                        jobnameExperience.getText().toString().trim(),
                        worksContentEdit.getText().toString().trim());
                break;

        }
    }



    public void save(String userId,
                     String jobId,
                     String  jobName,
                     String jobSex,
                     String jobPhone,
                     String jobEmail,
                     String jobProvince,
                     String jobCity,
                     String jobArea,
                     String jobEducation,
                     String jobExprience,
                     String jobDesctiption
                     ){
            int sex=0;
             if(jobSex.equals("男")){
                  sex=0;
             }else if(jobSex.equals("女")){
                 sex=1;
             }
        loadingdialog.show();
         Map<String,String> map=new HashMap<>();
         map.put("userId",""+userId);
        map.put("jobId",""+jobId);
        map.put("jobName",""+jobName);
        map.put("jobSex",""+sex);
        map.put("jobPhone",""+jobPhone);
        map.put("jobEmail",""+jobEmail);
        map.put("jobProvince",""+jobProvince);
        map.put("jobCity",""+jobCity);
        map.put("jobArea",""+jobArea);
        map.put("jobEducation",""+jobEducation);
        map.put("jobExprience",""+jobExprience);
        map.put("jobDesctiption",""+jobDesctiption);
        Xutils.POST(HttpUrl.userregisterjobadd, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("****************************save:"+result);
                loadingdialog.dismiss();
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    Toast.makeText(getApplicationContext(),""+jsonObject.getString("message"),Toast.LENGTH_SHORT).show();
                 switch (jsonObject.getInt("state")){
                     case HttpCode.SUCCESS:
                         finish();
                         break;
                 }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                  Toast.makeText(getApplicationContext(),"请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
